package com.duobaoyu.dby.supply.biz.client.boundary.account;

import com.duobaoyu.dby.channel.boundary.bean.account.param.BalanceChangeDetailParam;
import com.duobaoyu.dby.channel.boundary.bean.account.param.BalanceInfoParam;
import com.duobaoyu.dby.channel.boundary.bean.account.result.BalanceChangeDetailResult;
import com.duobaoyu.dby.channel.boundary.bean.account.result.BalanceInfoResult;
import com.duobaoyu.dby.channel.boundary.bean.phone.param.OperatorInfoParam;
import com.duobaoyu.dby.channel.boundary.bean.phone.result.OperatorInfoResult;
import com.duobaoyu.dby.channel.boundary.template.SupplyChannelTemplate;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.bo.BalanceChangeDetailBO;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.bo.BalanceInfoBO;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.bo.OperatorInfoBO;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.dto.BalanceChangeDetailDTO;
import com.duobaoyu.dby.supply.biz.client.boundary.account.bean.dto.OperatorInfoDTO;
import com.duobaoyu.dby.supply.biz.converter.ChannelAccountConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName AccountBoundaryClient
 * @createTime 16:26:00 2023/10/10
 */
@Service
@RequiredArgsConstructor
public class AccountBoundaryClient {

    private final SupplyChannelTemplate supplyChannelTemplate;
    private final ChannelAccountConverter channelAccountConverter;

    /**
     * 获取渠道余额
     * @param channelCode
     * @return
     */
    public BalanceInfoBO channelBalanceInfo(String channelCode) {
        BalanceInfoParam balanceInfoParam = BalanceInfoParam.builder()
                .channelCode(channelCode)
                .build();
        BalanceInfoResult balanceInfo = supplyChannelTemplate.getBalanceInfo(balanceInfoParam);
        return channelAccountConverter.toBalanceInfo(balanceInfo);
    }

    /**
     * 获取渠道余额
     * @return
     */
    public List<BalanceInfoBO> channelBalanceInfos() {
        List<BalanceInfoResult> balanceInfos = supplyChannelTemplate.getBalanceInfos();
        return channelAccountConverter.toBalanceInfoList(balanceInfos);
    }

    /**
     * 获取渠道余额
     * @param channelCodes
     * @return
     */
    public List<BalanceInfoBO> channelBalanceInfos(Set<String> channelCodes) {
        Set<BalanceInfoParam> balanceInfoParams = channelCodes.stream().map(channelCode -> BalanceInfoParam.builder().channelCode(channelCode).build()).collect(Collectors.toSet());
        List<BalanceInfoResult> balanceInfos = supplyChannelTemplate.getBalanceInfos(balanceInfoParams);
        return channelAccountConverter.toBalanceInfoList(balanceInfos);
    }

    /**
     * 获取渠道余额明细
     * @param balanceChangeDetail
     * @return
     */
    public BalanceChangeDetailBO channelBalanceChangeDetail(BalanceChangeDetailDTO balanceChangeDetail) {
        BalanceChangeDetailParam param = channelAccountConverter.toBalanceChangeDetailParam(balanceChangeDetail);
        BalanceChangeDetailResult balanceChangeDetailResult = supplyChannelTemplate.balanceChangeDetail(param);
        return channelAccountConverter.toBalanceChangeDetail(balanceChangeDetailResult);
    }

    /**
     * 手机号运营商查询
     * @param operatorInfo
     * @return
     */
    public OperatorInfoBO phoneOperatorInfo(OperatorInfoDTO operatorInfo) {
        OperatorInfoParam param = channelAccountConverter.toOperatorInfoParam(operatorInfo);
        OperatorInfoResult phoneOperatorInfo = supplyChannelTemplate.getPhoneOperatorInfo(param);
        return channelAccountConverter.toOperatorInfo(phoneOperatorInfo);
    }
}
